package ar.edu.sovietrussia.bars;

import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.user.PersonService;
import ar.edu.sovietrussia.LibraryApplicationPage;
import ar.edu.sovietrussia.MyAuthenticatedWebSession;
import ar.edu.sovietrussia.customcomponents.login.LoginPanel;
import ar.edu.sovietrussia.customcomponents.searchbook.MainSearchComponent;
import ar.edu.sovietrussia.customcomponents.util.IDoReplaces;
import ar.edu.sovietrussia.customcomponents.util.IFeedbacker;
import ar.edu.sovietrussia.customcomponents.util.WrapperPanel;

/**
 */
public abstract class MenuBar extends Panel implements IDoReplaces, IFeedbacker {

    private static final long serialVersionUID = 1L;

    @SpringBean
    protected PersonService personService;

    public PersonService getPersonService() {
        return this.personService;
    }

    public void setPersonService(final PersonService personService) {
        this.personService = personService;
    }

    public MenuBar(final String id) {
        super(id);
        this.addComponents();
    }

    private void addComponents() {

        Link linkToHome = new Link("homeLink") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                MenuBar.this.onHomeLinkClick();
            }

        };
        Link newSearch = new Link("newSearch") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                MenuBar.this.onNewSearchLinkClick();
            }

        };

        Link logOut = new Link("logoutLink") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                MenuBar.this.onLogoutLinkClick();
            }
        };
        this.add(linkToHome);
        this.add(newSearch);
        this.add(logOut);
    }

    public void onHomeLinkClick() {
        this.setResponsePage(LibraryApplicationPage.class);
    }

    public void onLogoutLinkClick() {
        MyAuthenticatedWebSession session = (MyAuthenticatedWebSession) this.getSession();
        session.signOut();
        session.setAttribute("model", null);
        this.getReplaceableComponents().get("topPanel").replace(this.generateSignInPanel(WrapperPanel.INTERNALID));
    }

    public void onNewSearchLinkClick() {
        Component component = new MainSearchComponent(WrapperPanel.INTERNALID) {
            private static final long serialVersionUID = 1L;

            @Override
            public FeedbackPanel getFeedbackTarget() {
                return MenuBar.this.getFeedbackTarget();
            }

            @Override
            public Map<String, WrapperPanel> getReplaceableComponents() {
                return MenuBar.this.getReplaceableComponents();
            }

        };
        this.getReplaceableComponents().get("centerPanel").replace(component);
    }

    public Panel generateSignInPanel(final String id) {
        Panel result = new LoginPanel(id) {

            private static final long serialVersionUID = 1L;

            @Override
            public Map<String, WrapperPanel> getReplaceableComponents() {
                return MenuBar.this.getReplaceableComponents();
            }

            @Override
            public FeedbackPanel getFeedbackTarget() {
                return MenuBar.this.getFeedbackTarget();
            }
        };

        return result;
    }
}
